<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\BaseController;
use App\Interfaces\SettingInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class SettingController extends BaseController
{
    protected SettingInterface $setting;

    public function __construct(SettingInterface $setting)
    {
        $this->setting = $setting;
    }

    /**
     * 获取配置
     * @param Request $request
     * @return JsonResponse
     */
    public function getConfig(Request $request): JsonResponse
    {
        $loginUser = $request->get('userInfo');
        $tenantId = $loginUser['tenantId'];
        $field = $request->input('field');
        $res = $this->setting->getConfig($tenantId,$field);
        return $this->apiResult(['code'=>0,'data'=>$res,'message'=>'Success']);
    }

    /**
     * 修改配置
     * @param Request $request
     * @return JsonResponse
     */
    public function updateConfig(Request $request): JsonResponse
    {
        $loginUser = $request->get('userInfo');
        $input = $request->input();
        $field = $input['field'];
        $tenantId = $loginUser['tenantId'];
        $data['tenantId'] = $tenantId;
        $data['updateTime'] = dateTime();
        $data['field'] = $field;
        $data['data'] = json_encode($input,320);

        $res = $this->setting->updateConfig($data);
        if (!$res)
        {
            return $this->apiResult(['code'=>1,'message'=>'Fail']);
        }
        return $this->apiResult(['code'=>0,'data'=>$res,'message'=>'Success']);
    }
}
